package com.fjsh.config;

import cn.hutool.core.util.StrUtil;
import com.fjsh.interfaces.Locker;
import com.fjsh.bean.RedissonLocker;
import com.fjsh.properties.RedissonProperties;
import com.fjsh.utils.RedissonUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 注意：使用Redisson的微服务需要在配置中心添加Redisson配置
 */
@Configuration
@ConditionalOnClass(Config.class)
public class RedissonConfig {

    @Resource
    private RedissonProperties redssionProperties;

    /**
     * 哨兵模式自动装配
     * @return
     */
    @ConditionalOnProperty(name="spring.data.redisson.sentinel")
    @Bean(name = "redissonSentinel",destroyMethod = "shutdown")
    RedissonClient redissonSentinel() {
        Config config = new Config();
        SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
                .setMasterName(redssionProperties.getMasterName())
                .setTimeout(redssionProperties.getTimeout())
                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());

        if(StrUtil.isNotEmpty(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }
        return Redisson.create(config);
    }

    /**
     * 单机模式自动装配
     * @return
     */
    @ConditionalOnProperty(name="spring.data.redisson.address")
    @Bean(name = "redissonSingle",destroyMethod = "shutdown")
    RedissonClient redissonSingle() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redssionProperties.getAddress())
                .setTimeout(redssionProperties.getTimeout())
                .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());

        if(StrUtil.isNotEmpty(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }

        return Redisson.create(config);
    }

    /**
     * 装配locker类，并将实例注入到RedissLockUtil中
     * @return
     */
    @Bean
    Locker distributedLocker(@Qualifier("redissonSingle") RedissonClient redissonClient) {
        RedissonLocker locker = new RedissonLocker();
        locker.setRedissonClient(redissonClient);
        RedissonUtil.setLocker(locker);
        return locker;
    }

}